$(function(){
    $('#pageNo').val(2)
    $('nav#menu_left').mmenu();
    $('.userlist li').live('click', function(e){
        var id = e.currentTarget.attributes['data-id'].value;
        window.location.href = '/user/'+id;
    });
    $('#startAct').on('click', function(){
        window.location.href = '/create'
    });
    $('#loadMore').on('click', function(){
        var btn = $(this);
        AlertBox.showTip('loading...');
        $.post('/fetchUsers', {
            pageNo: $('#pageNo').val()
        }, function(res){
            AlertBox.hideTip();
            if(res.error){
                AlertBox.showTip('抱歉,出问题了');
            }else{
                if(res.users.length > 0){
                    $('#pageNo').val(res.pageNo)
                    _buildUserElems(res.users, function(html){
                        $('#content').append(html);
                    })
                }else{
                    //$('#pageNo').val(2)
                    btn.html('木有了，去结交更多朋友吧～');
                }
            }
        })
    })
});
function _buildUserElems(users, cb){
    var html = '';
    for(var i in users){
        var user = users[i];
        html += '<li data-id="'+user.id+'">\
                            <div class="user_l">\
                            <img class="img-rounded size_32" alt="10" src="/img/'+user.id+'">\
                            </div>\
                            <div class="user_r">\
                                    '+user.name+'\
                                    '+user.tags+'\
                                    <br/>\
                                <span class="cardinfo">'+user.position+' '+user.company+'</span>\
                            </div>\
                            <div class="clearfix"></div>\
                            <hr>\
                        </li>';
    }
    cb(html);
}